#!/usr/bin/env python
# -*- coding: utf-8 -*-
__title__ = '配置文档'
__author__ = 'HaiFeng'
__mtime__ = '20180821'

import yaml
import sys
from redis import Redis, ConnectionPool
# from pymongo import MongoClient
from sqlalchemy import create_engine
from sqlalchemy.engine import Engine
from color_log import Logger


class Config(object):

    def __init__(self, cfg_file: str):
        cfg = yaml.load(open(cfg_file, 'r', encoding='utf-8'))
        # self.csv_path = ''
        # if 'csv_path' in cfg:
        #     self.csv_path = cfg['csv_path']

        # self.mongo: MongoClient = None
        # if 'mongo_config' in cfg:
        #     cfg_mg = cfg['mongo_config']
        #     self.mongo = MongoClient(cfg_mg['host'], cfg_mg['port'])
        #     if cfg_mg['user'] and cfg_mg['pwd']:
        #         if not self.mongo.get_database(cfg_mg['db']).authenticate(cfg_mg['user'], cfg_mg['pwd']):
        #             self.log.error('mongodb connected fail.')
        #             sys.exit(-1)

        self.rds: Redis = None
        if 'redis_config' in cfg:
            pool = ConnectionPool(host=cfg['redis_config']['host'], port=cfg['redis_config']['port'], decode_responses=True)
            self.rds = Redis(connection_pool=pool)

        self.engine_postgres: Engine = None
        if 'postgres_config' in cfg:
            cfg_pg = cfg['postgres_config']
            self.engine_postgres = create_engine('postgresql://{}:{}@{}:{}/{}'.format(cfg_pg['user'], cfg_pg['pwd'], cfg_pg['host'], cfg_pg['port'], cfg_pg['db']))

        cfg_ctp = cfg['ctp_config']
        self.front_name = cfg_ctp['ctp_front']
        cfg_ctp_front = cfg_ctp['fronts'][self.front_name]
        self.front_trade = cfg_ctp_front['trade']
        self.front_quote = cfg_ctp_front['quote']
        self.broker = cfg_ctp_front['broker']
        self.investor = cfg_ctp['investor']
        self.pwd = cfg_ctp['password']
        self.log = Logger()
